package com.gitee.feizns.itrip.personal.biz.controller;

import com.gitee.feizns.itrip.personal.beans.dto.Dto;
import com.gitee.feizns.itrip.personal.beans.pojo.User;
import com.gitee.feizns.itrip.personal.beans.vo.Page;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptCommentAdd;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptGetCommentList;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetCommentList;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetCount;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetImg;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewHotelDesc;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewHotelLable;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewHotelScore;
import com.gitee.feizns.itrip.personal.biz.service.CommentService;
import com.gitee.feizns.itrip.personal.biz.service.HotelService;
import com.gitee.feizns.itrip.personal.biz.service.exception.FileUploadException;
import com.gitee.feizns.itrip.personal.utils.Utils;
import com.gitee.feizns.itrip.personal.utils.component.annotations.CurtUser;
import com.gitee.feizns.itrip.personal.utils.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static com.gitee.feizns.itrip.personal.biz.service.exception.FileUploadException.IMAGE_UPLOAD_FAIL;

/**
 * @author feizns
 * @since 2019/1/9 0009
 */
@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HotelService hotelService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/getimg/{commentId}")
    public Dto<List<ForViewGetImg>> getImg(@PathVariable Integer commentId) {
        return Dto.getSuccessDto("获取评论图片成功", commentService.getImg(commentId));
    }

    @PostMapping("/getcommentlist")
    public Dto<Page<ForViewGetCommentList>> getCommentList(@RequestBody ForAcceptGetCommentList condition) {
        return Dto.getSuccessDto(null, commentService.getCommentList(condition));
    }

    @GetMapping("/getcount/{hotelId}")
    public Dto<ForViewGetCount> getCount(@PathVariable Integer hotelId) {
        return Dto.getSuccessDto("获取酒店各类评论数成功", commentService.getCount(hotelId));
    }

    @GetMapping("/gethotelscore/{hotelId}")
    public Dto<ForViewHotelScore> getHotelScore(@PathVariable Integer hotelId) {
        return Dto.getSuccessDto("获取评分成功", commentService.getHotelScore(hotelId));
    }
    
    @GetMapping("/gethoteldesc/{hotelId}")
    public Dto<ForViewHotelDesc> getHotelDesc(@PathVariable Integer hotelId) {
        return Dto.getSuccessDto(null, hotelService.getHotelDesc(hotelId));
    }

    @GetMapping("/gettraveltype")
    public Dto<List<ForViewHotelLable>> getTravelType() {
        return Dto.getSuccessDto("获取旅游类型列表成功", commentService.getTravelType());
    }

    @PostMapping("/add")
    public Dto add(@RequestBody ForAcceptCommentAdd comment, @CurtUser User curtUser) {
        commentService.add(comment, curtUser.getId());
        return Dto.getSuccessDto("新增评论成功", null);
    }

    @PostMapping("/upload")
    public Dto<String[]> upload(MultipartFile file, @CurtUser User curtUser) {
        try {
            String[] images = commentService.uploadImg(file.getInputStream(), curtUser.getId(), Utils.getFileSuffix(file.getOriginalFilename()));
            return Dto.getSuccessDto("文件上传成功", images);
        } catch ( Exception e ) {
            throw new FileUploadException(IMAGE_UPLOAD_FAIL, "文件上传失败");
        }
    }



}

